<%// Copyright 2008 Carnegie Mellon University
// Licensed under the Apache License, Version 2.0 (the "License"); 
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//  
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, 
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.%>

<!-- These two are the must -->
<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<%@ page pageEncoding="UTF-8" %>

<%@ page import="java.sql.*"%>
<%@ page import="java.util.List"%>
<%@ page import="java.util.HashMap"%>
<%@ page import="java.util.Map"%>
<%@ page import="edu.cmu.lti.sepia.data.resource.ResourceManager"%>
<%@ page import="edu.cmu.lti.sepia.data.db.SQL"%>
<%@ page import="edu.cmu.lti.sepia.data.db.LogDAO"%>
<%@ page import="edu.cmu.lti.sepia.data.db.TaskDAO"%>
<%@ page import="edu.cmu.lti.sepia.data.db.UserDAO"%>
<%@ page import="edu.cmu.lti.sepia.infra.system.model.Log"%>
<%@ page import="edu.cmu.lti.sepia.infra.system.model.User"%>
<%@ page import="edu.cmu.lti.sepia.infra.topic.model.Task"%>
<%@ page import="edu.cmu.lti.sepia.servlet.Commons"%>

<%
out.println( Commons.getHeader(request, response, "Log Browser", 1) );

SQL sql = new SQL();
List<Log> logs = LogDAO.getLogs( sql );
List<Task> tasks = TaskDAO.getTasks( sql );
List<User> users = UserDAO.getUsers( sql );
sql.close();
Map<Integer,String> tMap = new HashMap<Integer,String>();
for ( Task t : tasks ) {
  tMap.put( t.getTaskId(), t.getTaskNameShort() );
}
Map<Integer,String> uMap = new HashMap<Integer,String>();
for ( User u : users ) {
	uMap.put( u.getUserId(), u.getUserName() );
}
%>

<table cellpadding="0" cellspacing="0" border="0" class="datatable">
<thead>
<tr>
<th>Log Id</th>
<th>Date</th>
<th>Task Id</th>
<th>User Id</th>
<th>Host</th>
<th>Event</th>
<th>Detail</th>
</tr>
</thead>
<tbody>
<%
for (int i=0;i<logs.size();i++){
    Log log = logs.get(i);
	out.println("<tr class=\""+(i%2==1?"odd":"even")+"\">");
    out.println("<td nowrap>" + log.getLogId() + "</td>");
    out.println("<td nowrap>" + log.getDate() + "</td>");
    out.println("<td nowrap>" + (tMap.get(log.getTaskId())!=null?tMap.get(log.getTaskId()):"-") + "</td>");
    out.println("<td nowrap>" + (uMap.get(log.getUserId())!=null?uMap.get(log.getUserId()):"-") + "</td>");
    out.println("<td nowrap>" + log.getHost() + "</td>");
    out.println("<td nowrap>" + log.getLogEvent() + "</td>");
    out.println("<td><div style=\"overflow:hidden;width:300px;height:15px;\">" + log.getDetail() + "</div></td>");
    out.println("</tr>");
}
%>
</tbody>
<tfoot>
	<tr>
		<th>Log Id</th>
		<th>Date</th>
		<th>Task Id</th>
		<th>User Id</th>
		<th>Host</th>
		<th>Event</th>
		<th>Detail</th>
	</tr>
</tfoot>
</table>

<%
out.println( Commons.getFooter(request, response) );
%>